package cn.com.nes.common.util;

import cn.com.nes.site.entity.DataReq;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ParameterUtil {

    public static String[] getStringArray(DataReq req, String paramName) {
        String[] paramValue = null;
        if (req.getReqData().get(paramName) instanceof String[]) {
            paramValue = (String[]) req.getReqData().get(paramName);
        }
        if (req.getReqData().get(paramName) instanceof String) {
            paramValue = new String[]{(String) req.getReqData().get(paramName)};
        }

        return paramValue != null && paramName.length() > 0 ? paramValue : null;
    }

    public static String getString(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        return paramValue != null ? paramValue : "";
    }

    public static Integer getInteger(DataReq req, String paramName) {
        final Object o = req.getReqData().get(paramName);
        if (o instanceof Integer) {
            return (Integer) o;
        } else if (o instanceof String) {
            return NumberUtil.isInteger((String) o) ? Integer.parseInt((String) o) : null;
        } else if (o != null && NumberUtil.isInteger(o.toString())) {
            return Integer.parseInt(o.toString());
        }
        return null;
    }

    public static Integer getInteger(DataReq req, String paramName, int defaultValue) {
        final Object o = req.getReqData().get(paramName);
        if (o instanceof Integer) {
            return (Integer) o;
        } else if (o instanceof String) {
            return NumberUtil.isInteger((String) o) ? Integer.parseInt((String) o) : defaultValue;
        } else if (o != null && NumberUtil.isInteger(o.toString())) {
            return Integer.parseInt(o.toString());
        }
        return defaultValue;
    }

    public static Byte getByte(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        return NumberUtil.isInteger(paramValue) ? Byte.parseByte(paramValue) : null;
    }

    public static Boolean getBoolean(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        if ("0".equals(paramValue.trim()) || "false".equals(paramName.trim())) {
            return false;
        } else if ("1".equals(paramValue.trim()) || "true".equals(paramValue.trim())) {
            return true;
        }
        return null;
    }

    public static Date getDateTime(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        return StrUtil.isNotBlank(paramValue) ? DateUtil.parseDateTime(paramValue) : null;
    }

    public static Date getDate(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        return StrUtil.isNotBlank(paramValue) ? DateUtil.parseDate(paramValue) : null;
    }

    public static Double getDouble(DataReq req, String paramName) {
        final String paramValue = (String) req.getReqData().get(paramName);
        return NumberUtil.isNumber(paramValue) ? Double.parseDouble(paramValue) : null;
    }

    public static BigDecimal getDecimal(DataReq req, String paramName) {
        final Object o = req.getReqData().get(paramName);
        if (o instanceof Number) {
            if (o instanceof BigDecimal) {
                return (BigDecimal) o;
            } else if (o instanceof Double) {
                return BigDecimal.valueOf((Double) o);
            } else if (o instanceof Float) {
                return BigDecimal.valueOf((Float) o);
            } else if (o instanceof Long) {
                return new BigDecimal((Long) o);
            } else if (o instanceof Integer) {
                return new BigDecimal((Integer) o);
            } else if (o instanceof Short) {
                return new BigDecimal((Short) o);
            } else if (o instanceof Byte) {
                return new BigDecimal((Byte) o);
            }
        } else if (o instanceof String) {
            final String paramValue = (String) o;
            return NumberUtil.isNumber(paramValue) ? new BigDecimal(paramValue) : null;
        }
        return null;
    }

    public static String getString(DataReq req, String prefix, String defaultValue) {
        String value = getString(req, prefix);
        return StrUtil.isBlank(value) ? defaultValue : value;
    }

    public static Long getLong(DataReq req, String paramName) throws Exception {
        final String paramValue = String.valueOf(req.getReqData().get(paramName));
        return NumberUtil.isLong(paramValue) ? Long.parseLong(paramValue) : null;
    }

    public static <T> T parse(DataReq req, Class<T> clazz) {
        final HashMap<String, Object> map = req.getReqData();
        return BeanUtil.mapToBean(map, clazz, true);
    }

    public static JSONObject toJSONObject(DataReq req) {
        return new JSONObject(req.getReqData());
    }

    public static JSONArray getJSONArray(DataReq req, String key) {
        return (JSONArray) req.getReqData().get(key);
    }

    public static String getJSONArrayToSplitStr(DataReq req, String key, String split) throws Exception {
        if (StrUtil.isEmpty(split)) {
            throw new Exception("请填写分隔符");
        }
        JSONArray jsonArray = (JSONArray) req.getReqData().get(key);
        // 将 JSON 数组字符串解析为 List<String>
        List<String> list = JSON.parseArray(jsonArray.toJSONString(), String.class);
        // 使用分号将 List 中的元素连接成字符串
        String splitStr = String.join(split, list);
        return splitStr;
    }

    public static String getDatetimeString(DataReq req, String key) {
        String datetime = getString(req, key);
        // 转为北京时间
        if (datetime.contains("T") && datetime.contains("Z") && datetime.contains(".")) {
            ZonedDateTime zonedDateTime = ZonedDateTime
                    .parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"))
                    .withZoneSameInstant(ZoneId.systemDefault());
            datetime = zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        return datetime;
    }
}
